package com.veo.controller;

import com.veo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

//    @GetMapping("/hello")
//    public String hello(Model model){
//        model.addAttribute("user","张三");
//        model.addAttribute("link","http://www.baidu.com");
//        return "index";
//    }

    @RequestMapping("/")
    public String toRoot(){
        return "login";
    }


    @RequestMapping({"/login"})
    public String toLogin(User user, HttpSession session,Model model){
         if (StringUtils.hasLength(user.getUsername()) && "666".equals(user.getPassword())){
             session.setAttribute("loginUser",user);
             return "redirect:/main.html";
         }
         model.addAttribute("msg","用户名或密码错误");
        return "login";
    }

    @GetMapping("/main.html")
    public String toMain(HttpSession session,Model model){
        Object user = session.getAttribute("loginUser");
        if (user != null){
            return "index";
        }
        model.addAttribute("msg","请先登录");
        return "login";
    }

    @GetMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/";
    }


}
